import { Component, OnInit } from '@angular/core';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { CheckService } from './check.service';
import { environment } from '../../../../../src/environments/environment';
import * as _ from 'lodash';
import { DataService } from '../../../services/data.service';
import { Util } from 'app/common/util';
@Component({
  selector: 'oldage-check',
  templateUrl: './check.component.html',
  styleUrls: ['./check.component.scss'],
  providers: [CheckService]
})
export class OldageCheckComponent implements OnInit {
  // lodash
  public _: any = _;

  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];
 //单个分页列表数据
 public data:any = {};
  // 列表是否全选
  public isCheckedAll = false;
  //养老基金审核列表数据
  public oldageCheckList: any;
  // 养老基金审核列表
  public oldageCheckBody: any = {
    pageIndex: 1,
    pageSize: 25,
    sFundType: 7,
    sFullName: '',
    sFundCode: '',
    sModifyItem: '',
    auditType: ''
  };
  // 养老基金审核列表分页配置
  public oldageCheckListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 25,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  // 字典值
  public dictionaryList: any = {
    RENEWAL_AUDIT_TYPE: [],
  };

  constructor(
    public oldageCheckService: CheckService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    let that = this;
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    this.list();
    // 权限处理
    this.dataService.initTree.subscribe(data => {
      if (data) {
        let SessionMenuData = sessionStorage.getItem('menu');
        this.menuData = JSON.parse(SessionMenuData);
        this.menuResourceSet = [];
        _.forEach(this.menuData, (item, index) => {
          if (item.sName == '养老基金') {
            _.forEach(item.children, (it, idx) => {
              if (it.sName == '修改审核') {
                that.menuResourceSet = it.resourceSet;
                sessionStorage.setItem('btn-of', this.menuResourceSet);
              }
            })
          }
        })
      }
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData);
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '养老基金') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '修改审核') {
            that.menuResourceSet = it.resourceSet;
            sessionStorage.setItem('btn-of', this.menuResourceSet);
          }
        })
      }
    })
  }
  ngAfterViewInit() {
    //分页列表排序
    // setTimeout(function () {
    //   window['$']('#list_table').DataTable({
    //     "paging": false,
    //     "lengthChange": false,
    //     "searching": false,
    //     "ordering": true,
    //     "info": false,
    //     "destroy": true,
    //     "columnDefs": [
    //       { "orderable": false, "targets": 0 }
    //     ],
    //     "order": [
    //       [0, null]
    //     ],
    //   });
    // }, 1000);
  }

  list(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    // 查询条件转码
    this.oldageCheckBody.pageIndex = pageNum ? pageNum : this.oldageCheckListPageInfo.currentPageNum;
    this.oldageCheckBody.pageSize = pageSize ? pageSize : this.oldageCheckListPageInfo.pageSize;
    this.oldageCheckService.list(this.oldageCheckBody).subscribe(data => {
      if (data) {
        that.oldageCheckList = data.list;
        that.oldageCheckListPageInfo.currentPageNum = data.pageNum;
        that.oldageCheckListPageInfo.totalPages = data.pages;
        that.oldageCheckListPageInfo.total = data.total;
        that.oldageCheckListPageInfo.startRow = data.startRow;
        that.oldageCheckListPageInfo.endRow = data.endRow;
        console.info(that.oldageCheckList);
      }
      //  // 给表格列支持列拖动效果
      //  setTimeout(() => {
      //   Util.colResizable("#fixed-table");
      // }, 100);

      // 给表格列支持列拖动和固定表头效果
       Util.colResizable("#checkList");
    })
  }
  // 养老基金审核列表切换页
  oldageCheckPagesizeChange(pageSize: number) {
    if (pageSize !== this.oldageCheckListPageInfo.pageSize) {
      this.oldageCheckListPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 养老基金审核列表分页
  oldageCheckPageNavigation(currentPageNum: number) {
    this.oldageCheckListPageInfo.currentPageNum = currentPageNum;
    this.list(this.oldageCheckListPageInfo.currentPageNum, this.oldageCheckListPageInfo.pageSize);
  }

  checkedAll() {
    if (this.isCheckedAll) { // 更新为全选
      _.forEach(this.oldageCheckList, item => {
        item.isChecked = true;
      });
    } else { // 更新为不全选
      _.forEach(this.oldageCheckList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.oldageCheckList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.oldageCheckList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }

  // 撤回
  checkWithdrew() {
    let that = this;
    let checkProduct = [];
    let nAuditNo: any;
    let sStatusList = [];

    _.forEach(this.oldageCheckList, item => {
      if (item.isChecked == true) {
        checkProduct.push(item);
      }
    })
    // 无勾选
    if (checkProduct.length < 1) {
      window['swal']("注意", "请选择一条修改记录后再进行撤回操作", "warning")
    }
    // 勾选一个
    if (checkProduct.length == 1) {
      _.forEach(checkProduct, ite => {
        nAuditNo = ite.nAuditNo;
        sStatusList.push(ite.sStatus);
      })
      if (sStatusList[0] == '1') {
        window["swal"]({
          title: "注意",
          text: "是否撤回合同的修改",
          type: "warning",
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          showCancelButton: true,
          closeOnConfirm: false,
          closeOnCancel: true,
          showLoaderOnConfirm: true
        },
          function (isConfirm) {
            if (isConfirm) {
              that.oldageCheckService.checkWithdrew(nAuditNo).subscribe(data => {
                if (data) {
                  window['swal']("成功", "已撤回!", "success");
                  that.list();
                }
              })
            }
          })
      } else {
        window['swal']("注意", "只能撤回待审核的记录!", "warning");
      }
    }
    // 勾选多个
    if (checkProduct.length > 1) {
      window['swal']("注意", "请选择一条修改记录后再进行撤回操作", "warning")
    }
  }
  // 审核通过
  checkPass() {
    let that = this;
    let checkProduct = [];
    let nFundNoList = [];
    let nAuditNoList = [];
    let sStatusList = [];
    let sTypeList = [];
    _.forEach(this.oldageCheckList, item => {
      if (item.isChecked == true) {
        checkProduct.push(item);
      }
    })
    // 无勾选
    if (checkProduct.length < 1) {
      window['swal']("注意", "请选择修改记录后再进行审核操作!", "warning")
    }
    // 勾选一个
    if (checkProduct.length == 1) {
      _.forEach(checkProduct, ite => {
        nFundNoList.push(ite.nFundNo);
        nAuditNoList.push(ite.nAuditNo);
        sStatusList.push(ite.sStatus);
      })
      if (sStatusList[0] == '2') {
        window['swal']("注意", "该记录已经审核通过,请勿重复审核操作!", "warning");
      } else {
        window["swal"]({
          title: "注意",
          text: "是否确定审核通过",
          type: "warning",
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          showCancelButton: true,
          closeOnConfirm: false,
          closeOnCancel: true,
          showLoaderOnConfirm: true
        },
          function (isConfirm) {
            if (isConfirm) {
              that.oldageCheckService.checkPass(nFundNoList, nAuditNoList).subscribe(data => {
                if (data) {
                  window['swal']("成功", "已审核!", "success");
                  that.list();
                }
              })
            }
          })
      }
    }
    // 勾选多个
    if (checkProduct.length > 1) {
      _.forEach(checkProduct, ite => {
        nFundNoList.push(ite.nFundNo);
        nAuditNoList.push(ite.nAuditNo);
        sStatusList.push(ite.sStatus);
        sTypeList.push(ite.sType);
      })
      if (_.includes(sStatusList, '2')) {
        window['swal']("注意", "勾选的记录中存在已经审核通过的记录,请勿重复审核操作!", "warning");
      } else {
        if (_.includes(sTypeList, '2')) {
          window['swal']("注意", "续约只能单笔审核，请重新选择!", "warning")
        } else {
          window["swal"]({
            title: "注意",
            text: "是否确定审核通过",
            type: "warning",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            showCancelButton: true,
            closeOnConfirm: false,
            closeOnCancel: true,
            showLoaderOnConfirm: true
          },
            function (isConfirm) {
              if (isConfirm) {
                that.oldageCheckService.checkPass(nFundNoList, nAuditNoList).subscribe(data => {
                  if (data) {
                    window['swal']("成功", "已审核!", "success");
                    that.list();
                  }
                })
              }
            })
        }
      }
    }
  }
  // 审核不通过
  checkNotPass() {
    let that = this;
    let checkProduct = [];
    let nFundNoList = [];
    let nAuditNoList = [];
    let sStatusList = [];
    let sTypeList = [];
    _.forEach(this.oldageCheckList, item => {
      if (item.isChecked == true) {
        checkProduct.push(item);
      }
    })
    // 无勾选
    if (checkProduct.length < 1) {
      window['swal']("注意", "请选择修改记录后再进行审核操作!", "warning")
    }
    // 勾选一个
    if (checkProduct.length == 1) {
      _.forEach(checkProduct, ite => {
        nFundNoList.push(ite.nFundNo);
        nAuditNoList.push(ite.nAuditNo);
        sStatusList.push(ite.sStatus);
      })
      if (sStatusList[0] == '2') {
        window['swal']("注意", "该记录已经审核通过,请勿重复审核操作!", "warning");
      } else {
        window["swal"]({
          title: "注意",
          text: "是否确定审核不通过",
          type: "warning",
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          showCancelButton: true,
          closeOnConfirm: false,
          closeOnCancel: true,
          showLoaderOnConfirm: true
        },
          function (isConfirm) {
            if (isConfirm) {
              that.oldageCheckService.checkNotPass(nFundNoList, nAuditNoList).subscribe(data => {
                if (data) {
                  window['swal']("成功", "已审核!", "success");
                  that.list();
                }
              })
            }
          })
      }
    }
    // 勾选多个
    if (checkProduct.length > 1) {
      _.forEach(checkProduct, ite => {
        nFundNoList.push(ite.nFundNo);
        nAuditNoList.push(ite.nAuditNo);
        sStatusList.push(ite.sStatus);
        sTypeList.push(ite.sType);
      })
      if (_.includes(sStatusList, '2')) {
        window['swal']("注意", "勾选的记录中存在已经审核通过的记录,请勿重复审核操作!", "warning");
      } else {
        if (_.includes(sTypeList, '2')) {
          window['swal']("注意", "续约只能单笔审核，请重新选择!", "warning")
        } else {
          window["swal"]({
            title: "注意",
            text: "是否确定审核不通过",
            type: "warning",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            showCancelButton: true,
            closeOnConfirm: false,
            closeOnCancel: true,
            showLoaderOnConfirm: true
          },
            function (isConfirm) {
              if (isConfirm) {
                that.oldageCheckService.checkNotPass(nFundNoList, nAuditNoList).subscribe(data => {
                  if (data) {
                    window['swal']("成功", "已审核!", "success");
                    that.list();
                  }
                })
              }
            })
        }
      }
    }
  }
  // 导出
  exportExcel() {
    let that = this;
    let checkedItem = [];
    _.forEach(this.oldageCheckList, item => {
      if (item.isChecked) {
        checkedItem.push(item.nAuditNo);
      }
    })
    let converItem: any = checkedItem.join(',');
    let getBody = {
      "nAuditNoList": converItem,
      "sFundType": this.oldageCheckBody.sFundType,
      "sFullName": this.oldageCheckBody.sFullName,
      "sFundCode": this.oldageCheckBody.sFundCode,
      "sModifyItem": this.oldageCheckBody.sModifyItem
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    getBody.sFullName = encodeURI(getBody.sFullName)
    iframe.attr('src', environment.server + '/pms/v1/pension/pensionAudit/export/auditInfo?token=' + sessionStorage.getItem('token') + '&nAuditNoList=' + getBody.nAuditNoList + '&sFullName=' + getBody.sFullName + '&sFundCode=' + getBody.sFundCode + '&sModifyItem=' + getBody.sModifyItem + '&sFundType=' + getBody.sFundType);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.oldageCheckList, [colName], ["asc"]);
    if (JSON.stringify(this.oldageCheckList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.oldageCheckList, [colName], ["desc"]);
    }
    this.oldageCheckList = listDataAsc;
  }
 // 显示单个数据模态框
 showInfoModal(item) {
  this.data = _.cloneDeep(item);
  window['$']('#checkInfo').modal('show');
}
}
